﻿using FlyingEye.Come;
using FlyingEye.Come.Repositories;
using FlyingEye.Exceptions;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Validation;

namespace FlyingEye.ComServices
{
    /// <summary>
    /// COEM 组通知服务
    /// </summary>
    public class ComeGroupNotificationConfigurationService : ApplicationService
    {
        private readonly IComeGroupNotificationConfigurationRepository _configurationRepository;

        public ComeGroupNotificationConfigurationService(IComeGroupNotificationConfigurationRepository comeGroupNotificationRepository)
        {
            _configurationRepository = comeGroupNotificationRepository;
        }

        public async Task<ComeGroupNotificationConfigurationResponse> GetAsync(Guid id)
        {
            var model = await _configurationRepository.GetAsync(id);

            return ObjectMapper.Map<ComeGroupNotificationConfigurationModel, ComeGroupNotificationConfigurationResponse>(model);
        }

        public async Task<List<ComeGroupNotificationConfigurationResponse>> GetListAsync()
        {
            var groups = await _configurationRepository.GetListAsync();

            if (groups.Any())
            {
                return ObjectMapper.Map<List<ComeGroupNotificationConfigurationModel>, List<ComeGroupNotificationConfigurationResponse>>(groups);
            }

            return new List<ComeGroupNotificationConfigurationResponse>(0);
        }

        public async Task<ComeGroupNotificationConfigurationResponse> InsertAsync(CreateComeGroupNotificationConfigurationRequest configuration)
        {
            if (await _configurationRepository.AnyAsync(item => item.UniqueHashKey == configuration.UniqueHashKey))
            {
                throw new AbpValidationException("已存在相同的配置。");
            }

            if (await _configurationRepository.AnyAsync(item => item.GroupName == configuration.GroupName))
            {
                throw new AbpValidationException($"组名[{configuration.GroupName}]已经被使用。");
            }

            var model = ObjectMapper.Map<CreateComeGroupNotificationConfigurationRequest, ComeGroupNotificationConfigurationModel>(configuration);

            model = await _configurationRepository.InsertAsync(model);

            return ObjectMapper.Map<ComeGroupNotificationConfigurationModel, ComeGroupNotificationConfigurationResponse>(model);
        }

        public async Task<ComeGroupNotificationConfigurationResponse> UpdateAsync(UpdateComeGroupNotificationConfigurationRequest configuration)
        {
            if (await _configurationRepository.AnyAsync(item => item.UniqueHashKey == configuration.UniqueHashKey))
            {
                throw new AbpValidationException("已存在相同的配置。");
            }

            if (await _configurationRepository.AnyAsync(item => item.GroupName == configuration.GroupName))
            {
                throw new AbpValidationException($"组名[{configuration.GroupName}]已经被使用。");
            }

            var model = await _configurationRepository.FindAsync(configuration.Id);

            if (model == null)
            {
                throw new ResourceNotFoundException($"COME 组[{configuration.Id}]不存在。");
            }

            ObjectMapper.Map(configuration, model);

            model = await _configurationRepository.UpdateAsync(model);

            return ObjectMapper.Map<ComeGroupNotificationConfigurationModel, ComeGroupNotificationConfigurationResponse>(model);
        }

        public async Task DeleteAsync(Guid id)
        {
            if (!await _configurationRepository.AnyAsync(item => item.Id == id))
            {
                throw new ResourceNotFoundException($"COME 组[{id}]不存在。");
            }

            await _configurationRepository.DeleteAsync(id);
        }
    }
}
